<!--
 * createor: zc
 * email: zc8023zs@163.com
 * create_time: 2020/3/14 13:46:24
 *-->
<template>
  <el-container>
    <el-header style="margin:20px 10px 0 10px;height:auto">
      <el-form :inline="true" :model="listQuery">
        <el-form-item label="运营中心id:">
          <el-input v-model="listQuery.oc_id" clearable="clearable" maxlength="99999999999"></el-input>
        </el-form-item> 
        <el-form-item label="店铺/门店id:">
          <el-input v-model="listQuery.shop_id" clearable="clearable" maxlength="99999999999"></el-input>
        </el-form-item> 
        <el-form-item>
          <el-dropdown split-button type="primary" @click="onSearch" @command="onSearch" :loading="tabLoading">
            <i class="el-icon-search el-icon--left"></i>{{$t('table.search')}}
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command='all'>查询全部</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-form-item> 
        <el-form-item>
          <el-button type="primary" icon="el-icon-edit" @click="onAdd">{{$t('table.add')}}</el-button>
        </el-form-item> 
      </el-form>
    </el-header>
    <el-main>
      <el-table
        :key="tableKey"
        :data="listData" :height="tableHeight"
        highlight-current-row border fit
        v-loading="tabLoading"
      >
        <el-table-column type="index" label=" " width="50" align="center" :index="table_index" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="discount_coupon_id" label="标识id" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="oc_id" label="运营中心id" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="shop_id" label="店铺/门店id" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="full_money" label="红包金额" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="coupon_type" label="红包类型" width="120" align="center" :formatter="commentFormat" :show-overflow-tooltip="true"/>
        <el-table-column prop="off_money" label="满减条件" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="time_type" label="时间类型" width="120" align="center" :formatter="commentFormat" :show-overflow-tooltip="true"/>
        <el-table-column prop="start_time" label="开始时间" width="120" align="center" :formatter="dateFormat" :show-overflow-tooltip="true"/>
        <el-table-column prop="end_time" label="结束时间" width="120" align="center" :formatter="dateFormat" :show-overflow-tooltip="true"/>
        <el-table-column prop="duration" label="有效天数" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="create_time" label="创建时间" width="120" align="center" :formatter="dateFormat" :show-overflow-tooltip="true"/>
        <el-table-column prop="all_num" label="红包数量" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="off_num" label="已领数量" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="one_num" label="每人限领" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column prop="use_union" label="活动公用" width="120" align="center" :formatter="commentFormat" :show-overflow-tooltip="true"/>
        <el-table-column prop="enabled" label="是否可用" width="120" align="center" :show-overflow-tooltip="true"/>
        <el-table-column
          :label="$t('table.actions')"
          align="left"
          min-width="230"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-dropdown @command="onInfoPages">
              <span class="el-dropdown-link">
                <i class="el-icon-s-tools el-icon--left"></i>功能操作<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item icon="el-icon-edit" :command="{a:scope,b:'edit'}">{{$t('table.edit')}}</el-dropdown-item> 
                <el-dropdown-item icon="el-icon-search" :command="{a:scope,b:'info'}">{{$t('table.info')}}</el-dropdown-item> 
                <el-dropdown-item icon="el-icon-delete" :command="{a:scope,b:'delete'}">{{$t('table.delete')}}</el-dropdown-item> 
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="pagecnt>1"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.size"
        @pagination="getList"
      />
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="formData" label-width="200px" style="padding:5% 5% 0 5%">
          <input type="hidden" v-model="formData.discount_coupon_id"/>
        <el-form-item label="运营中心id：">
          <el-input v-model="formData.oc_id" placeholder="请输入运营中心id"></el-input>
        </el-form-item>
        <el-form-item label="店铺/门店id：">
          <el-input v-model="formData.shop_id" placeholder="请输入店铺/门店id"></el-input>
        </el-form-item>
        <el-form-item label="红包金额：">
          <el-input v-model="formData.full_money" placeholder="请输入红包金额"></el-input>
        </el-form-item>
        <el-form-item label="红包类型：">
            <el-select v-model="formData.coupon_type" placeholder="请选择">
                <el-option
                  v-for="item in coupon_typeSelectData"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="满减条件：">
          <el-input v-model="formData.off_money" placeholder="请输入满减条件"></el-input>
        </el-form-item>
        <el-form-item label="时间类型：">
            <el-select v-model="formData.time_type" placeholder="请选择">
                <el-option
                  v-for="item in time_typeSelectData"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="开始时间：">
          <el-input v-model="formData.start_time" placeholder="请输入开始时间"></el-input>
        </el-form-item>
        <el-form-item label="结束时间：">
          <el-input v-model="formData.end_time" placeholder="请输入结束时间"></el-input>
        </el-form-item>
        <el-form-item label="有效天数：">
          <el-input v-model="formData.duration" placeholder="请输入有效天数"></el-input>
        </el-form-item>
        <el-form-item label="创建时间：">
          <el-input v-model="formData.create_time" placeholder="请输入创建时间"></el-input>
        </el-form-item>
        <el-form-item label="红包数量：">
          <el-input v-model="formData.all_num" placeholder="请输入红包数量"></el-input>
        </el-form-item>
        <el-form-item label="已领数量：">
          <el-input v-model="formData.off_num" placeholder="请输入已领数量"></el-input>
        </el-form-item>
        <el-form-item label="每人限领：">
          <el-input v-model="formData.one_num" placeholder="请输入每人限领"></el-input>
        </el-form-item>
        <el-form-item label="活动公用：">
            <el-select v-model="formData.use_union" placeholder="请选择">
                <el-option
                  v-for="item in use_unionSelectData"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="是否可用：">
          <el-input v-model="formData.enabled" placeholder="请输入是否可用"></el-input>
        </el-form-item>
        <el-form-item label="数据版本字段：">
          <el-input v-model="formData.version_id" placeholder="请输入数据版本字段"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
          <el-button
            type="primary"
            v-if="curdAction=='update' || curdAction=='add'"
            @click="onSave"
            :loading="saveLoading" v-text="$t('table.save')"
          ></el-button> 
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    </el-main>
  </el-container>
</template>
<style scoped>
body .app-container .el-container,
body .app-container .el-header,
body .app-container .el-main,
body .el-container,
body .el-header,
body .el-main {
  padding: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
</style>
<script>
import Pagination from "@/components/Pagination";
import { Loading } from "element-ui";
import {
  qr_discount_couponSelecttree,
  qr_discount_couponSelectpage,
  qr_discount_couponSelect,
  qr_discount_couponAdd,
  qr_discount_couponUpdate,
  qr_discount_couponDel,
  qr_discount_couponInfo
} from '@/api/project/wmyw/project_wmyw_qr_discount_coupon'
export default {
  name: "a_wmywa_qr_discount_coupon",
  components: { Pagination, },
  filters: {},
  data() {
    return {
      dialogTitle: `优惠券优惠编辑`,
      curdAction: "",
      saveLoading: false,
      delLoading: false,
      tabLoading: false,
      dialogFormVisible: false,
      tableKey: 'wmyw_qr_discount_coupon_table',
      listData: null,
      total: 0,
      pagecnt: 0,
      tableHeight: (window.innerHeight-210),
      listQuery: {
        page: 1,
        size: 20,
        oc_id: null, 
        shop_id: null, 
      },
      formData: {
        discount_coupon_id: null, 
        oc_id: null, 
        shop_id: null, 
        full_money: null, 
        coupon_type: null, 
        off_money: null, 
        time_type: null, 
        start_time: null, 
        end_time: null, 
        duration: null, 
        create_time: null, 
        all_num: null, 
        off_num: null, 
        one_num: null, 
        use_union: null, 
        enabled: null, 
        version_id: null, 
      },
      coupon_typeSelectData: [
        {value: 1,label: '无门槛'},
        {value: 2,label: '满减'},
      ],
      time_typeSelectData: [
        {value: 1,label: '固定日期'},
        {value: 2,label: '按领券日起'},
      ],
      use_unionSelectData: [
        {value: 1,label: '公用'},
        {value: 2,label: '单独'},
      ],
    };
  },
  mounted(){
    let that =this;
    window.onresize=()=>{
      this.tableHeight = (window.innerHeight-210);
    }
  },
  created() {
    this.getList();
  },
  methods: {
    onSearch(command){
      if(command && command=='all'){
        this.resetQuery();
      }
      this.listQuery.page=1;
      this.getList();
    },
    onInfoPage(scope){
      var view = this.$store.state.permission.routesMap["/wmyw/qr_discount_coupon_info"];
      this.$router.push({ path: view.path, query: {m_id:view.code_aes,id:scope.row.discount_coupon_id} })
    },
    onInfoPages(commend){
      if(commend.b=='edit')this.onEdit(commend.a);
      if(commend.b=='info')this.onInfoPage(commend.a);
      if(commend.b=='delete')this.onDel(commend.a);
    },
    resetQuery(){
      this.listQuery = {
        page: 1,
        size: 20,
        oc_id: null, 
        shop_id: null, 
      }
    },
    reset(){
      this.formData = {
        discount_coupon_id: null, 
        oc_id: null, 
        shop_id: null, 
        full_money: null, 
        coupon_type: null, 
        off_money: null, 
        time_type: null, 
        start_time: null, 
        end_time: null, 
        duration: null, 
        create_time: null, 
        all_num: null, 
        off_num: null, 
        one_num: null, 
        use_union: null, 
        enabled: null, 
        version_id: null, 
      };
    },
    onAdd(){
      this.dialogFormVisible=true;
      this.curdAction = "add";
      this.dialogTitle=`优惠券优惠添加`;
      this.reset();
    },
    onEdit(data){
      this.onInfo(data);
      this.dialogFormVisible=true;
      this.curdAction = "update";
      this.dialogTitle=`优惠券优惠编辑`;
      this.reset();
    },
    onSave(){
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.saveLoading = true;
          if (this.curdAction == "add") {
            qr_discount_couponAdd(this.$qs.stringify(this.formData)).then(r => {
                this.getList();
                this.reset();
                this.curdAction = '';
                this.saveLoading  = false;
                this.dialogFormVisible = false
            }).catch(()=>{
              this.saveLoading=false;
            });
          } else if (this.curdAction == "update") {
            qr_discount_couponUpdate(this.$qs.stringify(this.formData)).then(r => {
                this.getList();
                this.reset();
                this.curdAction = '';
                this.saveLoading  = false;
                this.dialogFormVisible = false
            }).catch(()=>{
              this.saveLoading=false;
            });
          }
        }
      });
    },
    onDel(data){
        var that = this;
        this.$confirm('确定删除?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            qr_discount_couponDel(that.$qs.stringify({id:data.row.discount_coupon_id})).then(r=>{
                that.getList();
                that.reset();
                that.curdAction = '';
                that.delLoading  = false;
                that.$notify({
                    message:"删除成功",
                    type: "success"
                });
            }); 
        }).catch(() => {
            this.$notify({
                message: "已取消删除",
                type: "info"
            }); 
        });
    },
    onInfo(data){
        qr_discount_couponInfo({id:data.row.discount_coupon_id}).then(r=>{
            this.formData.discount_coupon_id=r.dataSingle.discount_coupon_id;
            this.formData.oc_id=r.dataSingle.oc_id;
            this.formData.shop_id=r.dataSingle.shop_id;
            this.formData.full_money=r.dataSingle.full_money;
            this.formData.coupon_type=r.dataSingle.coupon_type;
            this.formData.off_money=r.dataSingle.off_money;
            this.formData.time_type=r.dataSingle.time_type;
            this.formData.start_time=r.dataSingle.start_time;
            this.formData.end_time=r.dataSingle.end_time;
            this.formData.duration=r.dataSingle.duration;
            this.formData.create_time=r.dataSingle.create_time;
            this.formData.all_num=r.dataSingle.all_num;
            this.formData.off_num=r.dataSingle.off_num;
            this.formData.one_num=r.dataSingle.one_num;
            this.formData.use_union=r.dataSingle.use_union;
            this.formData.enabled=r.dataSingle.enabled;
            this.formData.version_id=r.dataSingle.version_id;
        });
    },
    dateFormat(row, column, cellValue, index){
      return this.$moment(cellValue).format("YYYY/MM/DD");
    }, 
    commentFormat(row, column, cellValue, index){
      try {
        var comment = "";
        if(column.property=='coupon_type'){
          comment='红包类型:无门槛=1 满减=2';
        }
        if(column.property=='time_type'){
          comment='时间类型:固定日期=1 按领券日起=2';
        }
        if(column.property=='use_union'){
          comment='活动公用:公用=1 单独=2';
        }
        var array = comment.split(':')[1].split(' ');
        var obj = {};
        for(var i=0;i<array.length;i++){
          var split = array[i].split('=');
          obj[split[1]]=split[0];
        }
        if(obj[cellValue]) return obj[cellValue];
        return cellValue;
      } catch (error) {
        return cellValue;
      }
    },
    table_index(index) {
      return (this.listQuery.page - 1) * this.listQuery.size + index + 1;
    },
    getList() {
      this.tabLoading=true;
      qr_discount_couponSelectpage(this.$qs.stringify(this.listQuery,{ indices: false })).then(r=>{
        this.listData = r.dataArray.dataList;
        this.total = r.dataArray.dataCount;
        this.pagecnt = r.dataArray.pageCount;
        this.tabLoading=false;
      }).catch(()=>{
        this.tabLoading=false;
      });
    }
  }
};
</script>